package com.atguigu.tingshu.order.config.rabbitMQ;

import com.atguigu.tingshu.common.constant.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 账户初始化RabbitMQ配置
 * @author Haoran Sun
 * @since 2024/08/27
 */

@Configuration
public class OrderInfoRabbitConfig {

    @Bean
    public Exchange orderExchange(){
        return ExchangeBuilder
                .directExchange(RabbitConstant.ORDER_EXCHANGE)
                .build();
    }

    @Bean
    public Queue delayOrderQueue(){
        return QueueBuilder
                .durable(RabbitConstant.ORDER_DELAY_QUEUE)
                .deadLetterExchange(RabbitConstant.ORDER_EXCHANGE)
                .deadLetterRoutingKey(RabbitConstant.ORDER_CANCEL_KEY)
                .ttl(RabbitConstant.ORDER_DELAY_TIME)
                .build();
    }

    @Bean
    public Queue orderCancelQueue(){
        return QueueBuilder
                .durable(RabbitConstant.ORDER_CANCEL_QUEUE)
                .build();
    }
    @Bean
    public Binding deadOrderBinding(
            @Qualifier("orderExchange") Exchange orderExchange,
            @Qualifier("orderCancelQueue") Queue orderQueue
    ){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderExchange)
                .with(RabbitConstant.ORDER_CANCEL_KEY)
                .noargs();
    }




    @Bean
    public Binding orderBinding(
            @Qualifier("orderExchange") Exchange orderExchange,
            @Qualifier("delayOrderQueue") Queue orderQueue
    ){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderExchange)
                .with(RabbitConstant.ORDER_DELAY_KEY)
                .noargs();
    }


    @Bean
    public Queue orderSuccessQueue(){
        return QueueBuilder
                .durable(RabbitConstant.ORDER_SUCCESS_QUEUE)
                .build();
    }

    @Bean
    public Binding orderSuccessBinding(
            @Qualifier("orderExchange") Exchange orderExchange,
            @Qualifier("orderSuccessQueue") Queue orderSuccessQueue
    ){
        return BindingBuilder
                .bind(orderSuccessQueue)
                .to(orderExchange)
                .with(RabbitConstant.ORDER_SUCCESS_KEY)
                .noargs();
    }


}
